import axios from 'axios';

export interface MenuItem {
  id: string | number;
  name: string;
  path: string;
  icon?: string;
  parentId?: string | number | null;
  orderNum?: number;
  status: number | string; // 后端返回的状态可能是字符串类型
  children?: MenuItem[];
  createTime?: string;
  updateTime?: string;
  component?: string;
  redirect?: string | null;
  perms?: string;
  isHidden?: number;
  isCache?: number;
  remark?: string;
}

export interface ApiResponse<T> {
  code: number;
  message: string;
  data: T;
}

// 获取菜单列表
export function getMenuList() {
  return axios.get<ApiResponse<MenuItem[]>>('/api/menu/list', {
    headers: {
      'Content-Type': 'application/json',
      // 如果需要认证，请添加认证头
      // 'Authorization': `Bearer ${localStorage.getItem('token')}`
    }
  });
}

// 添加菜单
export function addMenu(data: Omit<MenuItem, 'id'>) {
  return axios.post<ApiResponse<MenuItem>>('/api/menu/add', data, {
    headers: {
      'Content-Type': 'application/json',
      // 如果需要认证，请添加认证头
      // 'Authorization': `Bearer ${localStorage.getItem('token')}`
    }
  });
}

// 更新菜单
export function updateMenu(data: MenuItem) {
  console.log('调用updateMenu API，数据:', data);
  return axios.put<ApiResponse<boolean>>('/api/menu/update', data, {
    headers: {
      'Content-Type': 'application/json',
      // 认证头会由请求拦截器自动添加
    }
  }).then(response => {
    console.log('updateMenu API响应:', response);
    return response;
  }).catch(error => {
    console.error('updateMenu API错误:', error);
    throw error;
  });
}

// 删除菜单
export function deleteMenu(menuId: string | number) {
  return axios.delete<ApiResponse<boolean>>(`/api/menu/${menuId}`, {
    headers: {
      'Content-Type': 'application/json',
      // 如果需要认证，请添加认证头
      // 'Authorization': `Bearer ${localStorage.getItem('token')}`
    }
  });
} 